package com.example.demo.core.component;


/**
 * @author yj
 * @date 2020/9/16
 */

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.example.demo.core.exception.ProgramException;
import com.example.demo.core.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class SmsComponent {

    private Logger logger = LoggerFactory.getLogger(SmsComponent.class);

    private static final String regionId;
    private static final String accessKeyId;
    private static final String accessSecret;
    private static final DefaultProfile profile;
    private static final String domain;
    private static final String version;
    private static final String action;
    private static final String singName;
    private static final String templateCode;

    static {
        version = "2017-05.17";
        action = "SendSms";
        singName = "singName";
        templateCode = "templateCode";
        domain = "dysmsapi.aliyuncs.com";
        regionId = "cn-hangzhou";
        accessKeyId = "accessKeyId";
        accessSecret = "accessSecret";
        profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
    }


    public void sendCivilSms(String phone, String code) {
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(domain);
        request.setVersion(version);
        request.setAction(action);
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", singName);
        request.putQueryParameter("TemplateCode", templateCode);
        Map<String, String> map = new HashMap<String, String>();
        map.put("code", code);
        request.putQueryParameter("TemplateParam", map.toString());
        try {
            CommonResponse response = client.getCommonResponse(request);
            SendSmsResponse result = JsonUtil.jsonObjectToType(response.getData(), SendSmsResponse.class);
            if (result != null) {
                if ("OK".equals(result.getCode())) {
                    logger.info("发送成功");
                } else {
                    //错误码请查证
                    //https://help.aliyun.com/document_detail/101346.html?spm=a2c1g.8271268.10000.140.772fdf25pWHnIw
                    logger.error("短信发送错误，错误码：{},接收失败的的手机号码：{},发送code：{}", result.getCode(), phone, code);
                    throw new ProgramException("发送失败");
                }
            }
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}